package com.mx.kn.cstf2.actions;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import kn.cstf2.transition.Bean.TransitionBean;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.mx.kn.cstf2.dts.FilesUploadDts;
import com.mx.kn.cstf2.service.TransitionService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Component("multipleFileUploadAction")
@Scope(value = "session")
public class MultipleFileUploadAction extends ActionSupport implements
		ServletRequestAware {

	private File customerProfileFile;
	private File rfcFile;
	private File r1R2File;
	private File salesCallFile;
	private File customerQuotationFile;

	private String customerProfileFileFileName;
	private String rfcFileFileName;
	private String r1R2FileFileName;
	private String salesCallFileFileName;
	private String customerQuotationFileFileName;

	private String rateTracker;
	private String aconDebtorNumber;

	private TransitionBean transitionBean;
	private String filePath;
	private HttpServletRequest servletRequest;

	@Autowired
	TransitionService transitionService;



	public String uploadCustom() throws Exception {
		iniciar();

		// aqui se debe verificar el tipo de archivo (ver si se puede hacer por
		// interceptores);

		String guardaName = "";
		File fileToCreate = null;
		if (this.getCustomerProfileFile() != null) {
			//
			guardaName = transitionBean.getId() + "_"
					+ this.getCustomerProfileFileFileName();
			fileToCreate = new File(filePath, guardaName);
			FileUtils.copyFile(this.getCustomerProfileFile(), fileToCreate);
			transitionService.updateUploadedFile(
					FilesUploadDts.CUSTOMER_PROFILE, transitionBean.getId(),
					guardaName);
		}

		return SUCCESS;

	}

	public String uploadRFC() throws Exception {

		iniciar();

		String guardaName = "";
		File fileToCreate = null;
		if (this.getRfcFile() != null) {
			//
			guardaName = transitionBean.getId() + "_"
					+ this.getRfcFileFileName();
			fileToCreate = new File(filePath, guardaName);
			FileUtils.copyFile(this.getRfcFile(), fileToCreate);
			transitionService.updateUploadedFile(FilesUploadDts.RFC,
					transitionBean.getId(), guardaName);
		}

		return SUCCESS;
	}

	public String uploadR1R2() throws Exception {

		iniciar();

		String guardaName = "";
		File fileToCreate = null;
		if (this.getR1R2File() != null) {
			//
			guardaName = transitionBean.getId() + "_"
					+ this.getR1R2FileFileName();
			fileToCreate = new File(filePath, guardaName);
			FileUtils.copyFile(this.getR1R2File(), fileToCreate);
			transitionService.updateUploadedFile(FilesUploadDts.R1_R2,
					transitionBean.getId(), guardaName);
		}

		return SUCCESS;
	}

	public String uploadSalesCall() throws Exception {

		iniciar();

		String guardaName = "";
		File fileToCreate = null;
		if (this.getSalesCallFile() != null) {
			//
			guardaName = transitionBean.getId() + "_"
					+ this.getSalesCallFileFileName();
			fileToCreate = new File(filePath, guardaName);
			FileUtils.copyFile(this.getSalesCallFile(), fileToCreate);
			transitionService.updateUploadedFile(FilesUploadDts.SALES_CALL,
					transitionBean.getId(), guardaName);
		}

		return SUCCESS;
	}

	public String uploadCustomerQuotation() throws Exception {

		iniciar();

		String guardaName = "";
		File fileToCreate = null;
		if (this.getCustomerQuotationFile() != null) {
			//
			guardaName = transitionBean.getId() + "_"
					+ this.getCustomerQuotationFileFileName();
			fileToCreate = new File(filePath, guardaName);
			FileUtils.copyFile(this.getCustomerQuotationFile(), fileToCreate);
			transitionService.updateUploadedFile(
					FilesUploadDts.CUSTOMER_QUOTATION, transitionBean.getId(),
					guardaName);
		}

		return SUCCESS;
	}

	public String display() {
		return NONE;
	}

	private void iniciar() {
		filePath = FilesUploadDts.RUTA_FILES;

		ActionContext contexto = ActionContext.getContext();
		Map<String, Object> sesion = contexto.getSession();
		transitionBean = (TransitionBean) sesion.get("Knkey");
	}

	
	
	
	
	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		servletRequest = request;

	}

	public File getCustomerProfileFile() {
		return customerProfileFile;
	}

	public void setCustomerProfileFile(File customerProfileFile) {
		this.customerProfileFile = customerProfileFile;
	}

	public File getRfcFile() {
		return rfcFile;
	}

	public void setRfcFile(File rfcFile) {
		this.rfcFile = rfcFile;
	}

	public File getR1R2File() {
		return r1R2File;
	}

	public void setR1R2File(File r1r2File) {
		r1R2File = r1r2File;
	}

	public File getSalesCallFile() {
		return salesCallFile;
	}

	public void setSalesCallFile(File salesCallFile) {
		this.salesCallFile = salesCallFile;
	}

	public String getCustomerProfileFileFileName() {
		return customerProfileFileFileName;
	}

	public void setCustomerProfileFileFileName(
			String customerProfileFileFileName) {
		this.customerProfileFileFileName = customerProfileFileFileName;
	}

	public String getRfcFileFileName() {
		return rfcFileFileName;
	}

	public void setRfcFileFileName(String rfcFileFileName) {
		this.rfcFileFileName = rfcFileFileName;
	}

	public String getR1R2FileFileName() {
		return r1R2FileFileName;
	}

	public void setR1R2FileFileName(String r1r2FileFileName) {
		r1R2FileFileName = r1r2FileFileName;
	}

	public String getSalesCallFileFileName() {
		return salesCallFileFileName;
	}

	public void setSalesCallFileFileName(String salesCallFileFileName) {
		this.salesCallFileFileName = salesCallFileFileName;
	}

	public File getCustomerQuotationFile() {
		return customerQuotationFile;
	}

	public void setCustomerQuotationFile(File customerQuotationFile) {
		this.customerQuotationFile = customerQuotationFile;
	}

	public String getCustomerQuotationFileFileName() {
		return customerQuotationFileFileName;
	}

	public void setCustomerQuotationFileFileName(
			String customerQuotationFileFileName) {
		this.customerQuotationFileFileName = customerQuotationFileFileName;
	}

	public String getRateTracker() {
		return rateTracker;
	}

	public void setRateTracker(String rateTracker) {
		this.rateTracker = rateTracker;
	}

	public String getAconDebtorNumber() {
		return aconDebtorNumber;
	}

	public void setAconDebtorNumber(String aconDebtorNumber) {
		this.aconDebtorNumber = aconDebtorNumber;
	}

}
